<?php
class DB
{
    private $connection;
    /**
     * Настройки подлючения
     */
    private $defaults = array(
        'dsn' => 'mysql:host=localhost;dbname=dribble',
        'user' => 'root',
        'password' => ''
    );

    function __construct()
    {
        try {
            $this->connection = new PDO($this->defaults['dsn'], $this->defaults['user'], $this->defaults['password']);
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
    }

    public function getOne($condition, $params = array())
    {
        $statement = $this->execute($condition, $params);
        return $statement->fetch(PDO::FETCH_OBJ);
    }

    public function getAll($condition, $params = array())
    {
        $statement = $this->execute($condition, $params);
        return $statement->fetchAll(PDO::FETCH_OBJ);
    }

    /**
     * @param $query
     * @param $params
     * @return PDOStatement
     */
    public function execute($query, $params = null)
    {
        $statement = $this->connection->prepare($query);
//        if (isset($params)){
//            foreach ($params as $key => $param) {
//                if (is_array($param))
//                    $statement->bindParam($key, $param['name'], $param['type']);
//                else
//                    $statement->bindParam($key, $param);
//            }
//        }
        $statement->execute($params);
        return $statement;
    }

}